$(document).ready(function (){
require(['inputdrop'] ,function (inputdrop){
	var field;
	um_ajax_get({
		url:"rpc/getCodeList",
		paramObj:{key:"sec_event_field"},
		isLoad:"true",
		maskObj:"body",
		successCallBack:function(data){
			field = data.sec_event_field;
			policy_list();
		}
	});

	var list_url = "siem/eventgen/querySecuEventList";
	var list_col = [
						{name:"",text:"" ,hideSearch:"hide",width:3},
						{text:'安全策略名称',name:"alarmName",align:"left",width:26},
						{text:'匹配规则',name:"matchCriteriaValue",align:"left",width:26,searchRender:function (el){
							var data = [];
					  		for (var i = 0; i < field.length; i++) {
					  			data.push({text:field[i].codename,id:field[i].codevalue});
					  		}
							g_formel.multipleSelect_render(el ,{
								data : data,
								name : "matchCriteria",
								allowAll : true
							});
						}},
						{text:'最大限定时间(秒)',name:"alarmTimeout",align:"left",width:15,searchRender:function (el){
				  			index_render_div(el ,{type:"range",startKey:"minLimit",endKey:"alarmTimeout",title:"最大限定时间(秒)"});
				  		}},
						{text:'最大聚合次数',name:"countThreshold",align:"left",width:15,searchRender:function (el){
					  		index_render_div(el ,{type:"range",startKey:"minThreshold",endKey:"countThreshold",title:"最大聚合次数"});
					  	}},
						{text:'事件频率(条/秒)',name:"alarmFrequencies",align:"left",width:15,hideSearch:true}
				   ];
	var index_oper = [
						{icon:"rh-icon rh-edit" ,text:"修改" ,aclick:edit_template_init},
					 ];

	var update_url = "siem/eventgen/updSecuEventGen";

	var import_url = "siem/eventgen/secEventGenImport";


	event_init();

	function event_init()
	{
		$("#export_btn").click(function (){
			export_init();
		});

		$("#import_btn").click(function (){
			import_init();
		});

		index_list_search_render($("um-search") ,{
			cbf : function (val){
				g_grid.refresh($("#table_div") ,{
					paramObj : {alarmName : val}
				})
			},
			placeholder : "请输入策略名称"
		})
	}

	function policy_list()
	{
		g_grid.render($("#table_div"),{
			 header:list_col,
			 oper: index_oper,
			 operWidth:"60px",
			 dbIndex:1,
			 url:list_url,
			 isLoad : true,
			 maskObj : "body",
			 dbClick : detail_template_init,
			 hasBorder : false,
			 autoHeight : true,
			 showCount : true
		});
	}

	function edit_template_init(rowData)
	{
		$.ajax({
			type: "GET",
			url: "module/policy_manage/event_create_policy_tpl.html",
			success :function(data)
			{
				g_dialog.dialog($(data).find("[id=edit_template]"),{
					width:"560px",
					init:init,
					title : "事件生成策略修改",
					saveclick:save_click,
					autoHeight:"autoHeight",
					autoHeightValue:"15%"
				});
			}
		});

		function init(el)
		{
			g_validate.init(el);
			var el_alarmTimeout = el.find("[data-id=alarmTimeout]");
			var el_countThreshold = el.find("[data-id=countThreshold]");
			var el_minThreshold = el.find("[data-id=minThreshold]");
			var el_minLimit = el.find("[data-id=minLimit]");
			var sec_event_field = new HashMap();
			for (var i = 0; i < field.length; i++) {
				sec_event_field.put(field[i].codename,field[i].codevalue);
			}
			el.find("[name=matchCriteria]").each(function(){
				var name = $(this).attr("data-name");
				$(this).attr("value", sec_event_field.get(name));
			});
			el.umDataBind("render",rowData);
			var matchCriteria = rowData.matchCriteria.split(",");
			for (var i = 0; i < matchCriteria.length; i++) {
				el.find("[name=matchCriteria][value="+matchCriteria[i]+"]").prop("checked","checked");
			}	
			el_alarmTimeout.blur(function (){
				if($(this).val() == "")
				{
					return false;
				}
				if(parseInt(el_minLimit.val()) > parseInt($(this).val()) && el_minLimit.val() != "")
				{
					g_validate.setError($("[data-id=minLimit]"),"不能大于最大限定时间。");
				}
				else
				{
					g_validate.clear([el_minLimit]);
				}
			});
			el_countThreshold.blur(function (){
				if($(this).val() == "")
				{
					return false;
				}
				if(parseInt(el_minThreshold.val()) > parseInt($(this).val()) && el_countThreshold.val() != "")
				{
					g_validate.setError($("[data-id=minThreshold]"),"不能大于最大聚合次数。");
				}
				else
				{
					g_validate.clear([el_minThreshold]);
				}
			});
			el_minThreshold.blur(function (){
				if($(this).val() == "")
				{
					return false;
				}
				if(parseInt($(this).val()) > parseInt(el_countThreshold.val()) && el_countThreshold.val() != "")
				{
					g_validate.setError($("[data-id=minThreshold]"),"不能大于最大聚合次数。");
				}
				else
				{
					g_validate.clear([$(this)]);
				}
			});
			el_minLimit.blur(function (){
				if($(this).val() == "")
				{
					return false;
				}
				if(parseInt($(this).val()) > parseInt(el_alarmTimeout.val()) && el_alarmTimeout.val() != "")
				{
					g_validate.setError($("[data-id=minLimit]") ,"不能大于最大限定时间。");
				}
				else
				{
					g_validate.clear([$(this)]);
				}
			});	
		}

		function save_click(el ,saveObj)
		{
			if (!g_validate.validate(el))
			{
				return false;
			}

			if(el.find("[name=matchCriteria]:checked").size() == 0) 
			{
				g_dialog.operateAlert(el, "请至少选择一种规则匹配。" ,"error");
				return false;
			} 
			saveObj = $.extend(true, rowData,saveObj);
			um_ajax_post({
				url : update_url,
				paramObj : saveObj,
				maskObj : "body",
				successCallBack : function (data){
					g_dialog.operateAlert();
					g_dialog.hide(el);
					g_grid.refresh($("#table_div"));
				}
			});
		}
	}

	function detail_template_init(rowData)
	{
		$.ajax({
			type: "GET",
			url: "module/policy_manage/event_create_policy_tpl.html",
			success :function(data)
			{
				g_dialog.dialog($(data).find("[id=detail_template]"),{
					width:"500px",
					init:init,
					title:"时间生成策略详细信息",
					isDetail : true
				});
			}
		});

		function init(el)
		{
			el.umDataBind("render" ,rowData);
			el.find("[data-id=alarmTimeout]").text(rowData.alarmTimeout+" (秒)");
			el.find("[data-id=alarmFrequencies]").text(rowData.alarmFrequencies+" (条/秒)");
		}
	}

	function export_init()
	{
		var queryObj = $("[class=um-grid-search-tr]").umDataBind("serialize" ,"search-data");
		var queryJson = JsonTools.encode(queryObj);
		var array = g_grid.getIdArray($("#table_div") ,{chk:true ,attr:"alarmId"});
		window.location.href = index_web_app + index_soc_prefix + "siem/eventgen/exportSEGXml" + "?asset="+array.join(",")+"&queryStr="+encodeURI(queryJson);
	}
	function import_init()
	{
		$.ajax({
			type: "GET",
			url: "module/policy_manage/event_create_policy_tpl.html",
			success :function(data)
			{
				g_dialog.dialog($(data).find("[id=import_template]"),{
					width:"500px",
					init:init,
					title:"事件生成策略导入",
					saveclick: save_click
				});
			}
		});
		function init(el)
		{
			index_create_upload_el(el.find("[id=ptMap]"));
			el.find("[class=icon-trash]").click(function (){
				el.find("[data-id=up_name]").val("");
			});
		}
		function save_click(el,saveObj)
		{
			var validate_result = g_validate.fileSuffixLimit(el.find('[id=ptMap]').val(),"xml",true,true);
			if(!validate_result.flag){
				g_dialog.operateAlert(el,validate_result.msg,"error");
				return false;
			}

			var el_form = el.find("form");

			um_ajax_file(el_form,{
				url : import_url,
				successCallBack : function(){
					g_dialog.operateAlert();
					g_dialog.hide(el);
					policy_list();
				}
			});
				
		}

	}
});
});